package com.ebeijia.quartz;

import com.ebeijia.dto.RcvblDetDto;
import com.ebeijia.mybatis.pojo.BillInfo;
import com.ebeijia.mybatis.pojo.BillPushInfo;
import com.ebeijia.mybatis.pojo.UserInfo;
import com.ebeijia.service.admin.BillInfoService;
import com.ebeijia.service.admin.BillPushInfoService;
import com.ebeijia.service.wechat.impl.TemplateMessageService;
import com.ebeijia.util.PropertiesUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YPJ on 2016/5/24.
 */
@Component("pushQuartz")
@Lazy(false)
public class PushQuartz {

    @Resource
    private BillPushInfoService billPushInfoService;

    @Resource
    private BillInfoService billInfoService;

    @Resource
    private TemplateMessageService templateMessageService;

    /*  @Scheduled(cron = "0/30 * * * * ?")*/
    public void sendPush() {
        //获取待推送的用户信息
        List<BillPushInfo> billPushInfos = billPushInfoService.selectByAll();
        for (BillPushInfo billPushInfo : billPushInfos) {
            Map<String, Object> map = new HashMap<>();
            map.put("userCode", billPushInfo.getUserNo());
            billPushInfoService.callPushInfo(map);
            if (map.get("RESULT").toString().equals("1")) {
                RcvblDetDto rcvblDetDto = (RcvblDetDto)map.get("rcvblDet");
                BillInfo billInfo = new BillInfo();
                billInfo.setBillPushId(billPushInfo.getId());
                billInfo.setBillNo(rcvblDetDto.getBillNo());
                billInfo.setBillAmt(rcvblDetDto.getUmaActualAmt());
                billInfo.setBillWaterCons(rcvblDetDto.getUmaTotalFlux().toString());
                billInfo.setBillLastNum(rcvblDetDto.getUmaStartFlux().toString());
                billInfo.setBillThisNum(rcvblDetDto.getUmaEndFlux().toString());
                billInfo.setLastReadTime(rcvblDetDto.getUmaLastReaderDate());
                billInfo.setBillStatus(rcvblDetDto.getUmaChargeOffFlag());
                billInfo.setBillTime(rcvblDetDto.getUmaMonthEndDate());
                billInfo.setPushStaus("01");
                billInfoService.insertBillInfo(billInfo);
            }

        }

        List<BillInfo> billInfoList = billInfoService.selectNotPush();
        for (BillInfo billInfo : billInfoList) {
            String templateUnpaidBillFirst = "尊敬的客户您好，户号【" + billInfo.getBillNo() + "】，您最近产生的水费情况如下:";
            StringBuffer remark = new StringBuffer("注：此信息为当期水费产生后的提醒信息，供您参考,\n" + "上次抄表水量：" + billInfo.getBillLastNum()
                + "吨\n" + "本次抄表水量：" + billInfo.getBillThisNum() + "吨\n" + "抄表时间："
                + billInfo.getLastReadTime().toString() + "\n" + "，请您尽快缴费。祝您生活愉快～");
            //发送模板消息
            templateMessageService.sendBillInfoMsg(billInfo.getOpenId(),
                PropertiesUtils.getProperties("weixin.msg.bill"),
                templateUnpaidBillFirst,
                remark.toString(),
                billInfo.getAddress(),
                billInfo.getBillAmt().toString(),
                billInfo.getBillWaterCons().toString());
        }

    }

}
